package trading_strategy.positions;

import trading_strategy.instruments.ListedInstrument;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gderoujoux
 * Date: 6 mai 2010
 * Time: 11:16:40
 * To change this template use File | Settings | File Templates.
 */
public class InstrumentPosition {
    final ListedInstrument instrument;
    int position;
    List<Trade> trades = new ArrayList<Trade>();
    private double transactioCost;

    public InstrumentPosition(ListedInstrument instrument) {
        this.instrument = instrument;
    }

    public void addTrade(Trade t) {
        synchronized (this) {
            trades.add(t);
            position += t.getSignedQty();
        }
    }
    public int getPosition() {
        synchronized (this) {
            return position;
        }
    }

    public ListedInstrument getInstrument() {
        return instrument;
    }

    public double getPnL() {
        double pnl = 0;
        for (Trade trade : trades) {
            pnl += -trade.getSignedQty() * trade.getPrice() * instrument.getMultiplier() - instrument.getMarket().getTransactionCosts().getCost(instrument, trade.getSignedQty());
        }
        return pnl;
    }

    public double getTransactioCost() {
        double cost = 0;
        for (Trade trade : trades) {
            cost += instrument.getMarket().getTransactionCosts().getCost(instrument, trade.getSignedQty());
        }
        return cost;
    }
}
